package shuogg.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Controller之间的参数传递:
 * 
 * addFlashAttribute的原理是放session里面, 然后使用完系统自动擦除
 * addAttribute的原理就是简单地url拼接, 就是?username=xiaoming&usersex=male之类的
 * 
 * Eclipse: http://localhost:8080/springMVC_test_Anno/re/test.do
 * IDEA: http://localhost:8080/re/test.do
 */
@Controller
@RequestMapping("/re")
public class RedirectController {

    @RequestMapping("/test")
    public String test(HttpServletRequest request, RedirectAttributes model) {
        model.addFlashAttribute("username", "xiaoming");
        model.addAttribute("usersex", "male");
        return "redirect:test1.do";
    }

    @RequestMapping("/test1")
    @ResponseBody
    public String test1(HttpServletRequest request, @ModelAttribute("username") String username) {
        System.out.println(username);
        String name = request.getParameter("username"); // 错误, 这样是获取不到flashAttribute的, 底层是session来的
        String sex  = request.getParameter("usersex");  // 正确, 因为addAttribute就是将url拼接法进行封装罢了 
        return sex;
    }
}
